#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        game.py
# Purpose:     Setup and Main Loop
# 
# Authors:     Chris Spooner, Chris La-Roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random
from Ball import Ball
from Paddle import Paddle

if pygame.mixer:
    pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)

player = Paddle(5, [screenWidth/2, screenHeight-100])
balls = []
maxBalls = 1

bgColor = 0,0,0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        elif event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_UP 
                or event.key == pygame.K_w):
                    player.direction("Up")
            elif (event.key == pygame.K_DOWN 
                or event.key == pygame.K_s):
                    player.direction("Down")
            elif (event.key == pygame.K_RIGHT 
                or event.key == pygame.K_d):
                    player.direction("Right")
            elif (event.key == pygame.K_LEFT 
                or event.key == pygame.K_a):
                    player.direction("Left")
        elif event.type == pygame.KEYUP:
            if (event.key == pygame.K_UP 
                or event.key == pygame.K_w):
                    player.direction("Stop Up")
            elif (event.key == pygame.K_DOWN 
                or event.key == pygame.K_s):
                    player.direction("Stop Down")
            elif (event.key == pygame.K_RIGHT 
                or event.key == pygame.K_d):
                    player.direction("Stop Right")
            elif (event.key == pygame.K_LEFT 
                or event.key == pygame.K_a):
                    player.direction("Stop Left")
    
    #Create Balls
    while len(balls) < maxBalls:
        ballSpeed = [random.randint(1,6), 
                     random.randint(1,6)]
        ballPos = [random.randint(100,screenWidth-100),
                   random.randint(100,screenHeight/2)]
        balls += [Ball(ballSpeed, ballPos)]
        
    #Move
    player.move()
    for ball in balls:
        ball.move()
    
    #Collisions With Walls, and Paddle
    player.collideWall(screenWidth, screenHeight)
    for ball in balls:
        ball.collideFloor(screenHeight)
        ball.collideWall(screenWidth,screenHeight)
        ball.collidePaddle(player)
    
    #--Collisions Between Balls    
    for first in range(0,len(balls)-2):
        for second in range(first+1, len(balls)-1):
            balls[first].collideBall(balls[second])
    
    #Remove Non-Living Balls
    for ball in balls:
        if not ball.living:
            balls.remove(ball)
    
    #Draw Everything to Screen
    screen.fill(bgColor)
    screen.blit(player.surface, player.rect)
    for ball in balls:
        screen.blit(ball.surface, ball.rect)
        
    clock.tick(30)
    print clock.get_fps()

     
    